package com.note.config;


import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName Swagger2Config
 * @Description TODO
 * @Author QiBin
 * @Date 2020/5/611:46
 * @Version 1.0
 **/
@Configuration
@EnableSwagger2
//@EnableSwaggerBootstrapUI
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class Swagger2Config {

//    @Value("${swagger.title}")
//    private String title;
//
//    @Value("${swagger.description}")
//    private String description;
//
//    @Value("${swagger.version}")
//    private String version;
//
//    @Value("${swagger.termsOfServiceUrl}")
//    private String termsOfServiceUrl;
//
//    @Value("${swagger.contact.name}")
//    private String name;
//
//    @Value("${swagger.contact.url}")
//    private String url;
//
//    @Value("${swagger.contact.email}")
//    private String email;

    private static Logger logger = LoggerFactory.getLogger(Swagger2Config.class);
    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeys = new ArrayList<>();
        apiKeys.add(new ApiKey("Authorization", "accessToken", "header"));
        return apiKeys;
    }

//    private List<SecurityContext> securityContexts() {
//        List<SecurityContext> securityContexts = new ArrayList<>();
//        securityContexts.add(SecurityContext.builder()
//                .securityReferences(defaultAuth())
//                .forPaths(PathSelectors.regex("^(?!auth).*$")).build());
//        return securityContexts;
//    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }

    @Bean
    public Docket createRestApi() {

        logger.info("加载Swagger2");
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(new ApiInfoBuilder()
//                        //标题
//                        .title("项目API接口")
//                        //版本信息
//                        .version("1.0")
//                        //描述消息
//                        .description("接口列表")
//                        .contact(new Contact("项目API接口","localhost:8801/auth/doc.html","xxxx@qq.com"))
//                        .license("mamba")
////                        .licenseUrl("http://www.xxxx.com/")
//                        .build())
//                //最终调用接口后会和paths拼接在一起
//                .pathMapping("/")
//                .select()
//                //包路径
//                .apis(RequestHandlerSelectors.basePackage("com/facebook/controller"))
//                //过滤的接口
//                .paths(PathSelectors.any())
//                .build();
        return  new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.note.*.controller"))
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//                .paths(PathSelectors.any())
                .build();

//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo()).select()
//                //扫描所有有注解的api，用这种方式更灵活
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//                //这个  是指定扫描包路径
//                //.apis(RequestHandlerSelectors.basePackage("com/facebook/controller"))
//                .paths(PathSelectors.any())
//                .build()
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts());
    }
    private ApiInfo apiInfo() {
//        return new ApiInfoBuilder()
//                .title(title)
//                .description("测试名称")
//                .termsOfServiceUrl(termsOfServiceUrl)
//                .contact(new Contact("项目API接口","http://localhost:8099/","xxxx@qq.com"))
//                .version(version)
//                .build();

        // 名称，站点，邮箱地址
        Contact contact = new Contact("simba1949", "http://localhost:8080/", "simba1949@outlook.com");
        return new ApiInfoBuilder()
                // 标题
                .title("测试")
                // 描述
                .description("测试")
                // 服务条款链接
                .termsOfServiceUrl("http://localhost:8080/")
                // 联系人
                .contact(contact)
                // 认证许可
                .license("Apache License Version 2.0")
                // 认证许可链接
                .licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE")
                // 版本
                .version("2.0")
                .build();

    }
}